package game.ui.controls
{
	import engine.core.*;
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.*;
	import game.ui.core.*;
	import game.ui.events.*;
	
	public class RxUIElement extends Sprite
	{
		protected var m_width:Number;
		protected var m_height:Number;
		protected var m_measuredWidth:Number;
		protected var m_measuredHeight:Number;
		protected var m_sizeChanged:Boolean = false;
		protected var m_initialized:Boolean = false;
		protected var m_displayStateInvalidated:Boolean = false;
		private var m_layer:int;
		private var m_pulseDir:int = 1;
		private var m_updateQueueCount:int = 0;
		private var m_filters:Array;
		private static var s_renderQueue:Vector.<RxUIElement>;
		private static var s_currentElement:int = 0;
		
		public function RxUIElement()
		{
			this.m_layer = RxUILayerConstants.NO_LAYER;
		}
		
		public function InvalidateDisplayState() : void
		{
			if (!s_renderQueue)
			{
				s_renderQueue = new Vector.<RxUIElement>;
				RxStage.GetStage().addEventListener(Event.RENDER, OnStageRender);
			}
			
			if (!this.m_displayStateInvalidated || !IsInRenderQueue(this))
			{
				RxStage.GetStage().invalidate();
				m_displayStateInvalidated = true;
				AddElementToRenderQueue(this);
			}
		}
		
		private static function IsInRenderQueue(element:RxUIElement) : Boolean
		{
			var index:int = s_renderQueue.indexOf(element);
			return index != -1 && index < s_currentElement;
		}
		
		private static function AddElementToRenderQueue(element:RxUIElement) : void
		{
			s_renderQueue.push(element);
		}
		
		private static function OnStageRender(e:Event) : void
		{
			for each(var element:RxUIElement in s_renderQueue)
			{
				element.Render();
				s_currentElement = s_currentElement + 1;
			}
			
			s_currentElement = 0;
			s_renderQueue.length = 0;
		}
		
		public function Render() : void
		{
			if (this.m_displayStateInvalidated)
			{
				UpdateDisplayState();
			}
			
			this.m_displayStateInvalidated = false;
			this.m_sizeChanged = false;
		}
		
		protected function UpdateDisplayState() : void
		{
		}
		
		public function GetLayer() : int
		{
			return this.m_layer;
		}
		
		public function SetLayer(layer:int) : void
		{
			if (m_layer == layer)
			{
				return;
			}
			
			if (this.m_layer == RxUILayerConstants.NO_LAYER && layer == RxUILayerConstants.NO_LAYER)
			{
				return;
			}
			
			this.m_layer = layer;
			if (layer == RxUILayerConstants.NO_LAYER)
			{
				this.OnRemovedFromStage();
				RxUI.RemoveElementFromUpdateQueue(this);
			}
			else
			{
				this.OnAddedToStage();
			}
			
			this.m_updateQueueCount = 0;
		}
		
		protected function OnRemovedFromStage() : void
		{
			
		}
		
		protected function OnAddedToStage() : void
		{
			
		}
		
		public function InvalidateSize() : void
		{
			this.InvalidateDisplayState();
			this.m_sizeChanged = true;
		}
		
		public function RemoveFromLayer() : void
		{
			if (this.m_layer == RxUILayerConstants.NO_LAYER)
			{
				return;
			}
			
			RxUI.RemoveElementFromLayer(this);
		}
		
		public function AddFilter(param1:BitmapFilter) : void
		{
			if (!this.m_filters)
			{
				this.m_filters = [];
			}
			if (this.m_filters.indexOf(param1) == -1)
			{
				this.m_filters.push(param1);
			}
			this.UpdateFilterArray();
		}
		
		private function UpdateFilterArray() : void
		{
			if (!this.m_filters)
			{
				return;
			}
			var _loc_1:* = this.m_filters.slice();
			filters = _loc_1;
		}
		
		protected function RemoveFromUpdateQueue() : void
		{
			this.m_updateQueueCount = this.m_updateQueueCount - 1;
			if (this.m_updateQueueCount <= 0)
			{
				RxUI.RemoveElementFromUpdateQueue(this);
				this.m_updateQueueCount = 0;
			}
		}
		
		protected function DispatchInitialized() : void
		{
			if (hasEventListener(RxUIEvent.INITIALIZED))
			{
				this.m_initialized = true;
				dispatchEvent(new RxUIEvent(RxUIEvent.INITIALIZED));
			}
		}
	}
}